package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Result struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// Ok 响应成功
func Ok(c *gin.Context, msg string, data interface{}) {

	c.JSON(http.StatusOK, Result{
		Code: 200,
		Msg:  msg,
		Data: data,
	})
}

// OkWithData 响应成功
func OkWithData(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Result{
		Code: 200,
		Msg:  "ok",
		Data: data,
	})
}

// Fail 响应失败
func Fail(c *gin.Context, code int, msg string) {

	c.JSON(http.StatusOK, Result{
		Code: code,
		Msg:  msg,
		Data: gin.H{},
	})
}

// FailNoRoute 404
func FailNoRoute(c *gin.Context) {
	c.JSON(http.StatusOK, Result{
		Code: 404,
		Msg:  "请求的路径不存在",
		Data: gin.H{},
	})
}

// FailSysError 500
func FailSysError(c *gin.Context) {
	c.JSON(http.StatusOK, Result{
		Code: 500,
		Msg:  "系统异常",
		Data: gin.H{},
	})
}

func FailWithDetails(c *gin.Context, httpStatueCode int, details interface{}) {
	c.JSON(http.StatusOK, Result{
		Code: httpStatueCode,
		Msg:  "请求参数错误",
		Data: details,
	})
}

func FailUnknownError(c *gin.Context) {
	c.JSON(http.StatusOK, Result{
		Code: -1,
		Msg:  "未知异常",
		Data: gin.H{},
	})
}
